// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.border;

import org.dengues.reports.editor.ReportColorConstants;
import org.dengues.reports.editor.design.utils.BorderUtil;
import org.dengues.reports.editor.design.utils.ColorManager;
import org.dengues.reports.editor.design.utils.ColorUtil;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-20 qiang.zhang $
 * 
 */
public class CellBorder extends LineBorder {

    public static int FROM_ROW = 0;

    public static int FROM_CELL = 1;

    private static final Insets DEFAULT_CROP = new Insets(2, 2, 2, 2);

    private static final Insets DEFAULTINSETS = new Insets(3, 3, 2, 2);

    private Insets paddingInsets = new Insets(DEFAULTINSETS);

    private Insets borderInsets;

    private int bottomFrom = FROM_CELL;

    private int topFrom = FROM_CELL;

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.border.LineBorder#getInsets(org.eclipse.draw2d.IFigure)
     */
    public Insets getInsets(IFigure figure) {
        if (borderInsets != null) {
            return new Insets(borderInsets).add(paddingInsets);
        }

        return new Insets(paddingInsets);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.border.LineBorder#getBorderInsets()
     */
    public Insets getBorderInsets() {
        if (borderInsets != null) {
            return new Insets(borderInsets);
        }

        return Figure.NO_INSETS;
    }

    /**
     * Sets the border insets.
     * 
     * @param borderInsets
     */
    public void setBorderInsets(Insets borderInsets) {
        this.borderInsets = borderInsets;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.border.LineBorder#setPaddingInsets(org.eclipse.draw2d.geometry.Insets)
     */
    public void setPaddingInsets(Insets in) {
        if (in == null || (in.left == 0 && in.right == 0 && in.top == 0 && in.bottom == 0)) {
            paddingInsets = new Insets(DEFAULTINSETS);
            return;
        }

        paddingInsets.top = in.top > DEFAULTINSETS.top ? in.top : DEFAULTINSETS.top;
        paddingInsets.bottom = in.bottom > DEFAULTINSETS.bottom ? in.bottom : DEFAULTINSETS.bottom;
        paddingInsets.left = in.left > DEFAULTINSETS.left ? in.left : DEFAULTINSETS.left;
        paddingInsets.right = in.right > DEFAULTINSETS.right ? in.right : DEFAULTINSETS.right;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "drawBorder".
     * 
     * @param figure
     * @param g
     * @param side
     * @param style
     * @param width
     * @param color
     * @param insets
     */
    protected void drawBorder(IFigure figure, Graphics g, int side, int style, int[] width, String color, Insets insets) {
        Rectangle r = figure.getBounds().getCopy().crop(DEFAULT_CROP).crop(insets);

        if (style != 0) {
            // set ForegroundColor with the given color
            g.setForegroundColor(ColorManager.getColor(ColorUtil.parseColor(color)));

            // if the border style is set to "double",
            // draw a double line with the given width and style of "solid"
            if (style == -2) {
                BorderUtil.drawDoubleLine(g, side, width, r);
            }
            // if the border style is set to "solid", "dotted" or "dashed",
            // draw a single line according to the give style and width
            else {
                BorderUtil.drawSingleLine(g, side, style, width, r);
            }
        } else {
            g.setForegroundColor(ReportColorConstants.ShadowLineColor);
            // if the border style is set to none, draw a default dot line in
            // black as default
            BorderUtil.drawDefaultLine(g, side, r);
        }

        g.restoreState();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getBottomFrom".
     * 
     * @return
     */
    public int getBottomFrom() {
        return bottomFrom;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setBottomFrom".
     * 
     * @param bottomFrom
     */
    public void setBottomFrom(int bottomFrom) {
        this.bottomFrom = bottomFrom;
    }

    /**
     * Getter for topFrom.
     * 
     * @return the topFrom
     */
    public int getTopFrom() {
        return this.topFrom;
    }

    /**
     * Sets the topFrom.
     * 
     * @param topFrom the topFrom to set
     */
    public void setTopFrom(int topFrom) {
        this.topFrom = topFrom;
    }
}
